package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.toolkit.RegexpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.PersonalInfoXML;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/7 11:25
 * 修改备注：
 * ================================================
 */
public class EditPersonalInfoActivity extends BaseActivity {

    @Bind(R.id.edit_per_info_edt)
    EditText editPerInfoEdt;
    @Bind(R.id.edit_per_info_tv_hint)
    TextView editPerInfoTvHint;
    private String type = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_edit_personal_info);
        setRightText("保存");
    }

    @Override
    public void initMember() {

        //手动设置maxLength为10
        InputFilter[] filters = {new InputFilter.LengthFilter(10)};
        type = (String) getIntent().getSerializableExtra("type");
        switch (type) {
            case "niceName": // 昵称
                setTitle("修改昵称");
                editPerInfoEdt.setHint("请填写昵称");
                editPerInfoEdt.setText(PersonalInfoXML.getNiceName(this));
                editPerInfoEdt.setFilters(filters);
                editPerInfoTvHint.setText("最多输入10个字符");
                editPerInfoTvHint.setVisibility(View.VISIBLE);
                break;
            case "trueName": // 真实姓名
                setTitle("修改真实姓名");
                editPerInfoEdt.setHint("请填写真实姓名");
                editPerInfoEdt.setText(PersonalInfoXML.getTrueName(this));
                editPerInfoEdt.setFilters(filters);
                editPerInfoTvHint.setText("最多输入10个字符");
                editPerInfoTvHint.setVisibility(View.VISIBLE);
                break;
            case "idCardNo": // 身份证号
                setTitle("修改身份证号");
                editPerInfoEdt.setHint("请填写身份证号");
                editPerInfoEdt.setText(PersonalInfoXML.getIdCardNO(this));
                String digits = "0123456789xyzXYZ";
                editPerInfoEdt.setKeyListener(DigitsKeyListener.getInstance(digits));
                editPerInfoTvHint.setText("");
                editPerInfoTvHint.setVisibility(View.VISIBLE);
                break;
            case "userEmail": // 邮箱
                setTitle("修改邮箱");
                editPerInfoEdt.setHint("请填写邮箱");
                editPerInfoEdt.setText(PersonalInfoXML.getUserEmail(this));
                editPerInfoEdt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                editPerInfoTvHint.setText("我们会将推荐的信息发送您的邮箱。");
                editPerInfoTvHint.setVisibility(View.VISIBLE);
                break;
            case "dimensions": // 规模
                setTitle("设置规模");
                editPerInfoEdt.setText(PersonalInfoXML.getDimensions(this));
                editPerInfoEdt.setHint("请填写规模");
                editPerInfoTvHint.setText("");
                editPerInfoTvHint.setVisibility(View.GONE);
                break;
        }

        editPerInfoEdt.setSelection(editPerInfoEdt.getText().length());//将光标移至文字末尾
        editPerInfoEdt.requestFocus();//获取焦点
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {
        if (StringUtil.isStrEmpty(editPerInfoEdt.getText().toString())) {
            showToast("请输入内容!");
            return;
        }

        if (type.equals("niceName") && editPerInfoEdt.getText().toString().length() > 10) {
            showToast("昵称长度不能大于10位");
            return;
        }

        if (type.equals("idCardNo") && !RegexpUtil.isRegexpValidate(editPerInfoEdt.getText().toString(), RegexpUtil.REGEXP_ID_CARD)) {
            showToast("请输入正确的身份证号码");
            return;
        }

        if (type.equals("userEmail") && !RegexpUtil.isRegexpValidate(editPerInfoEdt.getText().toString(), RegexpUtil.EMAIL_REGEXP)) {
            showToast("请输入正确的邮箱地址");
            return;
        }

        Intent intent = new Intent();
        intent.putExtra("edit", editPerInfoEdt.getText().toString());
        intent.putExtra("type", type);
        setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent，当然也可以自己定义新的Bundle
        finish();//此处一定要调用finish()方法
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
